<template>
  <div class="plugin-market-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">🔌 插件市场</span>
          <div class="header-actions">
            <el-input v-model="searchKeyword" placeholder="搜索插件" clearable style="width: 300px;" size="small">
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </el-input>
            <el-button type="primary" @click="handleDevelop">
              <el-icon><Tools /></el-icon>
              开发插件
            </el-button>
          </div>
        </div>
      </template>

      <!-- 插件分类 -->
      <el-tabs v-model="activeCategory" @tab-click="handleCategoryChange">
        <el-tab-pane label="全部插件" name="all">
          <el-badge :value="plugins.length" class="tab-badge" />
        </el-tab-pane>
        <el-tab-pane label="业务增强" name="business">
          <el-badge :value="12" class="tab-badge" />
        </el-tab-pane>
        <el-tab-pane label="数据分析" name="analytics">
          <el-badge :value="8" class="tab-badge" />
        </el-tab-pane>
        <el-tab-pane label="工具效率" name="tools">
          <el-badge :value="15" class="tab-badge" />
        </el-tab-pane>
        <el-tab-pane label="集成对接" name="integration">
          <el-badge :value="10" class="tab-badge" />
        </el-tab-pane>
        <el-tab-pane label="已安装" name="installed">
          <el-badge :value="installedCount" type="success" class="tab-badge" />
        </el-tab-pane>
      </el-tabs>

      <!-- 插件列表 -->
      <el-row :gutter="20" class="plugin-list">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="plugin in filteredPlugins" :key="plugin.id">
          <el-card class="plugin-card" shadow="hover">
            <div class="plugin-icon" :style="{ background: plugin.color }">
              <el-icon :size="48">
                <component :is="plugin.icon" />
              </el-icon>
            </div>
            <div class="plugin-info">
              <h3 class="plugin-name">{{ plugin.name }}</h3>
              <p class="plugin-desc">{{ plugin.description }}</p>
              <div class="plugin-meta">
                <el-tag size="small">{{ plugin.category }}</el-tag>
                <el-tag size="small" type="success">v{{ plugin.version }}</el-tag>
                <span class="meta-item">
                  <el-icon><Download /></el-icon>
                  {{ plugin.downloads }}
                </span>
                <span class="meta-item">
                  <el-icon><Star /></el-icon>
                  {{ plugin.rating }}
                </span>
              </div>
              <div class="plugin-author">
                <el-avatar :size="24" :src="plugin.author.avatar" />
                <span>{{ plugin.author.name }}</span>
              </div>
            </div>
            <div class="plugin-actions">
              <el-button 
                v-if="!plugin.installed" 
                type="primary" 
                @click="handleInstall(plugin)"
                :loading="plugin.installing"
              >
                <el-icon><Download /></el-icon>
                安装
              </el-button>
              <el-button 
                v-else 
                type="success" 
                disabled
              >
                <el-icon><Select /></el-icon>
                已安装
              </el-button>
              <el-button @click="handleViewDetail(plugin)">详情</el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="filteredPlugins.length"
        :page-sizes="[12, 24, 48]"
        layout="total, sizes, prev, pager, next"
        style="margin-top: 20px; justify-content: center;"
      />
    </el-card>

    <!-- 插件详情对话框 -->
    <el-dialog v-model="detailDialogVisible" :title="currentPlugin?.name" width="900px">
      <div class="plugin-detail" v-if="currentPlugin">
        <el-row :gutter="20">
          <el-col :span="16">
            <div class="detail-section">
              <h3>插件介绍</h3>
              <p>{{ currentPlugin.description }}</p>
              <div class="detail-content">
                <h4>功能特性</h4>
                <ul>
                  <li v-for="(feature, index) in currentPlugin.features" :key="index">
                    {{ feature }}
                  </li>
                </ul>
                <h4>更新日志</h4>
                <el-timeline>
                  <el-timeline-item 
                    v-for="log in currentPlugin.changelog" 
                    :key="log.version"
                    :timestamp="log.date"
                  >
                    <strong>v{{ log.version }}</strong>
                    <p>{{ log.content }}</p>
                  </el-timeline-item>
                </el-timeline>
              </div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="detail-sidebar">
              <div class="sidebar-section">
                <h4>插件信息</h4>
                <div class="info-item">
                  <span class="label">版本:</span>
                  <el-tag size="small">v{{ currentPlugin.version }}</el-tag>
                </div>
                <div class="info-item">
                  <span class="label">分类:</span>
                  <el-tag size="small">{{ currentPlugin.category }}</el-tag>
                </div>
                <div class="info-item">
                  <span class="label">下载量:</span>
                  <span>{{ currentPlugin.downloads }}</span>
                </div>
                <div class="info-item">
                  <span class="label">评分:</span>
                  <el-rate v-model="currentPlugin.rating" disabled show-score />
                </div>
                <div class="info-item">
                  <span class="label">大小:</span>
                  <span>{{ currentPlugin.size }}</span>
                </div>
              </div>
              <div class="sidebar-section">
                <h4>作者信息</h4>
                <div class="author-info">
                  <el-avatar :size="60" :src="currentPlugin.author.avatar" />
                  <div class="author-details">
                    <strong>{{ currentPlugin.author.name }}</strong>
                    <p>{{ currentPlugin.author.description }}</p>
                  </div>
                </div>
              </div>
              <div class="sidebar-section">
                <el-button 
                  type="primary" 
                  size="large" 
                  style="width: 100%;"
                  v-if="!currentPlugin.installed"
                  @click="handleInstall(currentPlugin)"
                >
                  <el-icon><Download /></el-icon>
                  安装插件
                </el-button>
                <el-button 
                  type="success" 
                  size="large" 
                  style="width: 100%;"
                  v-else
                  disabled
                >
                  <el-icon><Select /></el-icon>
                  已安装
                </el-button>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, markRaw } from 'vue'
import { ElMessage } from 'element-plus'
import {
  Search, Tools, Download, Star, Select,
  DataAnalysis, Setting, ChatDotRound, Connection,
  Document, Tickets, Promotion, TrendCharts
} from '@element-plus/icons-vue'

const searchKeyword = ref('')
const activeCategory = ref('all')
const detailDialogVisible = ref(false)
const currentPlugin = ref(null)

const pagination = ref({
  page: 1,
  size: 12
})

// 插件列表
const plugins = ref([
  {
    id: 1,
    name: '智能客服助手',
    description: 'AI驱动的智能客服系统，支持自动回复、意图识别、智能推荐',
    category: '业务增强',
    version: '1.2.0',
    downloads: 3456,
    rating: 4.8,
    size: '2.5 MB',
    installed: false,
    installing: false,
    color: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
    icon: markRaw(ChatDotRound),
    author: {
      name: '智能科技',
      avatar: '',
      description: '专注AI应用开发'
    },
    features: [
      '24小时在线智能客服',
      'AI意图识别，智能回复',
      '知识库自动学习',
      '多渠道接入（微信、APP、网页）',
      '客服数据分析报表'
    ],
    changelog: [
      { version: '1.2.0', date: '2024-01-15', content: '新增AI意图识别功能' },
      { version: '1.1.0', date: '2024-01-01', content: '优化回复速度' }
    ]
  },
  {
    id: 2,
    name: '数据看板插件',
    description: '实时数据大屏，支持自定义指标、多维度分析、数据钻取',
    category: '数据分析',
    version: '2.0.0',
    downloads: 2890,
    rating: 4.9,
    size: '3.8 MB',
    installed: true,
    installing: false,
    color: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
    icon: markRaw(DataAnalysis),
    author: {
      name: '数据分析团队',
      avatar: '',
      description: '专业数据可视化'
    },
    features: [
      '实时数据大屏展示',
      '30+可视化图表类型',
      '自定义指标配置',
      '数据钻取分析',
      '多维度数据对比'
    ],
    changelog: [
      { version: '2.0.0', date: '2024-01-20', content: '全新2.0版本，重构架构' },
      { version: '1.5.0', date: '2023-12-15', content: '新增数据钻取功能' }
    ]
  },
  {
    id: 3,
    name: '微信公众号集成',
    description: '一键集成微信公众号，支持消息推送、菜单配置、粉丝管理',
    category: '集成对接',
    version: '1.5.0',
    downloads: 5678,
    rating: 4.7,
    size: '1.8 MB',
    installed: false,
    installing: false,
    color: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
    icon: markRaw(Connection),
    author: {
      name: '微信开发团队',
      avatar: '',
      description: '官方认证开发者'
    },
    features: [
      '微信公众号一键绑定',
      '消息自动推送',
      '自定义菜单配置',
      '粉丝数据同步',
      '图文消息编辑'
    ],
    changelog: [
      { version: '1.5.0', date: '2024-01-18', content: '支持视频号功能' }
    ]
  },
  {
    id: 4,
    name: '电子发票插件',
    description: '支持电子发票开具、查验、归档，对接税务系统',
    category: '业务增强',
    version: '1.0.5',
    downloads: 1234,
    rating: 4.6,
    size: '2.1 MB',
    installed: false,
    installing: false,
    color: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
    icon: markRaw(Document),
    author: {
      name: '财税科技',
      avatar: '',
      description: '专业财税解决方案'
    },
    features: [
      '电子发票一键开具',
      '发票真伪查验',
      '发票自动归档',
      '对接税务系统',
      '发票统计分析'
    ],
    changelog: [
      { version: '1.0.5', date: '2024-01-10', content: '修复开票bug' }
    ]
  },
  {
    id: 5,
    name: '优惠券营销',
    description: '强大的优惠券营销工具，支持多种券类型、发放规则、使用统计',
    category: '业务增强',
    version: '2.1.0',
    downloads: 4321,
    rating: 4.9,
    size: '1.5 MB',
    installed: true,
    installing: false,
    color: 'linear-gradient(135deg, #fa709a 0%, #fee140 100%)',
    icon: markRaw(Tickets),
    author: {
      name: '营销工具',
      avatar: '',
      description: '专业营销解决方案'
    },
    features: [
      '10+券类型（满减、折扣、兑换等）',
      '灵活的发放规则',
      '精准的用户定向',
      '实时使用统计',
      '营销效果分析'
    ],
    changelog: [
      { version: '2.1.0', date: '2024-01-22', content: '新增组合券功能' }
    ]
  },
  {
    id: 6,
    name: '营销自动化',
    description: 'AI驱动的营销自动化工具，智能推荐、自动触发、效果追踪',
    category: '业务增强',
    version: '1.8.0',
    downloads: 2345,
    rating: 4.8,
    size: '3.2 MB',
    installed: false,
    installing: false,
    color: 'linear-gradient(135deg, #30cfd0 0%, #330867 100%)',
    icon: markRaw(Promotion),
    author: {
      name: 'AI营销',
      avatar: '',
      description: 'AI营销专家'
    },
    features: [
      'AI智能推荐商品',
      '自动化营销流程',
      '用户行为分析',
      '个性化内容推送',
      '营销效果实时追踪'
    ],
    changelog: [
      { version: '1.8.0', date: '2024-01-20', content: '优化AI推荐算法' }
    ]
  }
])

// 已安装数量
const installedCount = computed(() => {
  return plugins.value.filter(p => p.installed).length
})

// 过滤插件
const filteredPlugins = computed(() => {
  let result = plugins.value

  // 按分类过滤
  if (activeCategory.value !== 'all') {
    if (activeCategory.value === 'installed') {
      result = result.filter(p => p.installed)
    } else {
      result = result.filter(p => p.category === getCategoryName(activeCategory.value))
    }
  }

  // 按关键词搜索
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase()
    result = result.filter(p =>
      p.name.toLowerCase().includes(keyword) ||
      p.description.toLowerCase().includes(keyword)
    )
  }

  return result
})

const getCategoryName = (category) => {
  const map = {
    business: '业务增强',
    analytics: '数据分析',
    tools: '工具效率',
    integration: '集成对接'
  }
  return map[category] || category
}

const handleCategoryChange = () => {
  pagination.value.page = 1
}

const handleInstall = (plugin) => {
  plugin.installing = true
  setTimeout(() => {
    plugin.installed = true
    plugin.installing = false
    ElMessage.success(`插件 "${plugin.name}" 安装成功！`)
  }, 2000)
}

const handleViewDetail = (plugin) => {
  currentPlugin.value = plugin
  detailDialogVisible.value = true
}

const handleDevelop = () => {
  ElMessage.info('打开插件开发工具')
}
</script>

<style scoped>
.plugin-market-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.plugin-list {
  margin-top: 20px;
}

.plugin-card {
  margin-bottom: 20px;
  transition: all 0.3s;
  cursor: pointer;
}

.plugin-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.plugin-icon {
  width: 100px;
  height: 100px;
  margin: 0 auto 20px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.plugin-info {
  text-align: center;
}

.plugin-name {
  margin: 0 0 10px 0;
  font-size: 16px;
  font-weight: bold;
  color: #303133;
}

.plugin-desc {
  margin: 0 0 15px 0;
  font-size: 13px;
  color: #606266;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.plugin-meta {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 15px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 13px;
  color: #909399;
}

.plugin-author {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 15px;
  font-size: 13px;
  color: #606266;
}

.plugin-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.plugin-detail {
  max-height: 600px;
  overflow-y: auto;
}

.detail-section h3 {
  margin: 0 0 15px 0;
  color: #303133;
}

.detail-content {
  margin-top: 20px;
}

.detail-content h4 {
  margin: 20px 0 10px 0;
  color: #303133;
}

.detail-content ul {
  padding-left: 20px;
}

.detail-content li {
  margin: 8px 0;
  color: #606266;
}

.detail-sidebar {
  position: sticky;
  top: 20px;
}

.sidebar-section {
  margin-bottom: 20px;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
}

.sidebar-section h4 {
  margin: 0 0 15px 0;
  color: #303133;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.info-item .label {
  font-size: 14px;
  color: #606266;
}

.author-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.author-details {
  text-align: center;
}

.author-details strong {
  display: block;
  margin-bottom: 5px;
  color: #303133;
}

.author-details p {
  margin: 0;
  font-size: 13px;
  color: #909399;
}
</style>

